package com.example.day6;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;//ViewPager容器
    private TextView time_tv;//5秒倒计时
    private Button start;//开始体验按钮
    private MyPagerAdapter myPagerAdapter;//适配器
    private ArrayList<View> list=new ArrayList<>();//数据：4张View
    private ArrayList<ImageView> icon_list=new ArrayList<>();//小圆点图片
    private int current_item=0;//默认显示页
    private int time=5;//默认剩余时间
    private SharedPreferences preferences;//第2次打开不走向导页
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {//handler：自动轮播+5秒到技术
            super.handleMessage(msg);
            if(msg.what==101){//自动轮播
                viewPager.setCurrentItem(current_item++);//设置滚动下一页
            }else  if(msg.what==102){//5秒倒计时
                time_tv.setText("剩余时间"+(--time));
                if(time==0){//页面跳转
                    Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置二次打开不走向导页
        preferences= getSharedPreferences("welcome", MODE_PRIVATE);
        boolean flag=preferences.getBoolean("isdone",false);
        if(flag){//若是第二次打开直接页面跳转
            Intent intent=new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
        }
//        //TODO 加载视图
        initview();
//        //TODO 加载圆点
        initicon();
//        //TODO 加载数据
        initdata();
//        //TODO 设置适配器
        initadpter();
//        //TODO 设置事件监听
        initListener();
        SharedPreferences aaa = getSharedPreferences("welcome", MODE_PRIVATE);
        SharedPreferences.Editor welcome = aaa.edit();
        welcome.putBoolean("isdone",true);
        welcome.commit();


        //TODO 自动轮播
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(current_item!=list.size()){//不是最后一页
                    handler.sendEmptyMessage(101);//发送一个空的消息
                }else{
                    timer.cancel();//如果是最后一页，直接取消定时器
                }
            }
        },0,2000);

    }

    private void initListener() {
        //开始按钮点击事件
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
        //滑动viewPager的时候小圆点跟着走
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //当页面滑动的时候调用
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }
            //当页面被选择的时候
            @Override
            public void onPageSelected(int i) {//1
                //TODO 将所有圆点变白，当前变灰
                for(int j=0;j<icon_list.size();j++){
                    icon_list.get(j).setImageResource(R.mipmap.bq2);
                }
                icon_list.get(i).setImageResource(R.mipmap.bq1);

                //TODO 当最后一个页面的时候显示按钮,和显示倒计时
                if(i==icon_list.size()-1){
                    start.setVisibility(View.VISIBLE);
                    time_tv.setVisibility(View.VISIBLE);
//                    //TODO 倒计时更新剩余时间
                    final Timer timer=new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            if(time>0){
                                handler.sendEmptyMessage(102);
                            }else{
                                timer.cancel();
                            }
                        }
                    },0,1000);
                }else{
                    start.setVisibility(View.GONE);
                    time_tv.setVisibility(View.GONE);
                }
            }
            //当页面滑动状态改变的时候
            @Override
            public void onPageScrollStateChanged(int i) {
                switch(i){
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                }
            }
        });
    }

    private void initadpter() {
        myPagerAdapter=new MyPagerAdapter(list,this);
        viewPager.setAdapter(myPagerAdapter);
    }

    private void initview() {
        viewPager=findViewById(R.id.pager);
        start=findViewById(R.id.start);
        time_tv=findViewById(R.id.time_tv);
    }

    private void initdata() {
        ImageView imageView1= new ImageView(this);
        imageView1.setImageResource(R.mipmap.miao1);
        list.add(imageView1);
        ImageView imageView2= new ImageView(this);
        imageView2.setImageResource(R.mipmap.miao3);
        list.add(imageView2);
        ImageView imageView3= new ImageView(this);
        imageView3.setImageResource(R.mipmap.miao4);
        list.add(imageView3);
        ImageView imageView4= new ImageView(this);
        imageView4.setImageResource(R.mipmap.miao5);
        list.add(imageView4);
        ImageView imageView5= new ImageView(this);
        imageView5.setImageResource(R.mipmap.miao6);
        list.add(imageView5);
    }
    private void initicon() {
        icon_list.add((ImageView) findViewById(R.id.icon1));
        icon_list.add((ImageView) findViewById(R.id.icon2));
        icon_list.add((ImageView) findViewById(R.id.icon3));
        icon_list.add((ImageView) findViewById(R.id.icon4));
        icon_list.add((ImageView) findViewById(R.id.icon5));
    }

}
